package com.aiit.ssm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.aiit.ssm.model.ProcResult;
import com.aiit.ssm.model.User;
import com.aiit.ssm.service.UserServiceImp;
import com.mysql.jdbc.StringUtils;


@Controller
public class UserController{
	@Autowired  //这里等价： IUserService userService=new  UserServiceImp()
	 UserServiceImp userService;

	@RequestMapping(value="toRegist",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,String> toRegist(HttpServletRequest req,User user)
	{
		HttpSession session =req.getSession();
		/*System.out.println("请求到达这里，toRegist用户");
		String code=request.getParameter("code");
		
		String userName=request.getParameter("userName");
		String password=request.getParameter("password");
		User user =new User(userName,password,code);*/
		System.out.println("请求到达这里"+user);
		Map<String, String> ret=new HashMap<String,String>();

		if (StringUtils.isNullOrEmpty(user.getUserName())) {
			ret.put("type", "error");
			ret.put("msg", "用户名不能为空");
			
			return ret;
		}
		if (StringUtils.isNullOrEmpty(user.getUserPwd())) {
			ret.put("type", "error");
			ret.put("msg", "密码不能为空");
			return ret;
		}
		if (StringUtils.isNullOrEmpty(user.getCode())) {
			ret.put("type", "error");
			ret.put("msg", "验证码不能为空");
			return ret;
		}
		if (!(user.getCode().equalsIgnoreCase(session.getAttribute("code").toString()))) {  //忽略验证码大小写  
			
			System.out.println("验证码错误");
			ret.put("type", "error");
			ret.put("msg", "验证码错误");
			
			return ret;          
		}
	
		if(userService.userRegist(user)>0) {
			ret.put("type", "success");
			ret.put("msg", "注册成功");
			return ret;
		}
		
		return ret;

	}

	
	
	@RequestMapping("toLogin")
	@ResponseBody
	public ProcResult toLogin(HttpServletRequest req,User user)
	{//@RequestParam("userName")String userName,@RequestParam("userPwd")String userPwd
		System.out.println("请求到达这里，toLogin用户");
		HttpSession session =req.getSession();
		ModelAndView model=new ModelAndView();
		if (!(user.getCode().equalsIgnoreCase(session.getAttribute("code").toString()))) {  //忽略验证码大小写  	
			System.out.println("验证码错误");
			return ProcResult.fail().bind("msg", "验证码错误");	     
		}
		session.setAttribute("userName",user.getUserName());
		int iRet=userService.selectUserName(user.getUserName());
		System.out.println("请求到达这里，selectUserName："+iRet);		
		if(iRet>0) {	
			if(userService.userLogin(user)>0) {			
				model.setViewName("index");
				return ProcResult.success();
			}else {
				return ProcResult.fail().bind("error", "密码错误");
			}
		}else {
			return ProcResult.success().bind("iRet", iRet);//用户名不存在
		}
		
		
	}
	
	@RequestMapping("toFlsh")
	public ModelAndView toFlsh(@RequestParam("userName")String userName)
	{//@RequestParam("userName")String userName,@RequestParam("userPwd")String userPwd
		System.out.println("请求到达这里，toFlsh用户"+userName);
		ModelAndView model=new ModelAndView();
		//User user =new User(userName,userPwd);		
		//System.out.println("请求到达这里，toLogin用户"+user);
		model.addObject("userName",userName);		
		model.setViewName("index");
			return model;
		
	}
	
	@RequestMapping(value="toUserName",method=RequestMethod.POST)
	@ResponseBody
	public ProcResult checkName(@RequestParam("userName")String userName)
	{
		//@RequestParam("userName")String userName,@RequestParam("userPwd")String userPwd
		System.out.println("请求到达这里，toUserName用户"+userName);		
		int iRet=userService.selectUserName(userName);
		System.out.println("请求到达这里，toUserName用户"+iRet);		
		return ProcResult.success().bind("iRet", iRet);
	}
}
